package com.linux.huhx.thread3;

import java.util.concurrent.TimeUnit;

public class WaitThreadTest {
    public static void main(String[] args) throws Exception {
        Object object = new Object();
        new WaitThread(object).start();
        TimeUnit.SECONDS.sleep(2); // 2秒之后唤醒
        new NotifyThread(object).start();
    }

    static class WaitThread extends Thread {
        private Object object;

        WaitThread(Object object) {
            this.object = object;
        }

        @Override
        public void run() {
            synchronized (object) {
                try {
                    System.out.println("before wait.");
                    object.wait(1);
                    System.out.println("after wait.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class NotifyThread extends Thread {
        private Object object;

        NotifyThread(Object object) {
            this.object = object;
        }

        @Override
        public void run() {
            synchronized (object) {
                System.out.println("before notify.");
                object.notify();
                System.out.println("after notify.");
            }
        }
    }
}
